package com.gin.kotlin.no_gay_alliance.base

import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.gin.kotlin.no_gay_alliance.R

/**
 * 弹窗工具类
 */
class AlertDialogUtils {
    companion object {
        /**
         * 弹出一个确认框
         * @param context 上下文
         * @param title 标题
         * @param message String?
         * @param contentView View?
         * @param listener OnClickListener
         */
        fun builder(
            context: Context,
            title: String?=null,
            message: String? = null,
            contentView: View? = null,
            listener: DialogInterface.OnClickListener? = null
        ) =
            AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .also {
                    // 如果 contentView 非空，则添加到弹窗中，且设置弹窗关闭时移除 contentView
                    if (contentView != null) {
                        it.setView(contentView)
                        it.setOnDismissListener { (contentView.parent as ViewGroup).removeView(contentView) }
                    }
                }
                .setPositiveButton(R.string.alert_bottom_positive, listener)
                // 如果 listener 不为空，添加取消按钮
                .also { if (listener != null) it.setNegativeButton(R.string.alert_bottom_negative, listener) }
    }
}